package com.audaque.springboot.foshanupload.web.datajpademo.controller;


import com.audaque.springboot.foshanupload.web.datajpademo.model.qo.BlogQo;
import com.audaque.springboot.foshanupload.web.datajpademo.model.entity.Type;
import com.audaque.springboot.foshanupload.web.datajpademo.service.BlogService;
import com.audaque.springboot.foshanupload.web.datajpademo.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @Author: ONESTAR
 * @QQ群: 530311074
 * @URL: https://onestar.newstar.net.cn/
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable Long id, Model model) {
        List<Type> types = typeService.listTypeTop(10000);
        if (id == -1) {
           id = types.get(0).getId();
        }
        BlogQo blogQo = new BlogQo();
        blogQo.setTypeId(id);
        model.addAttribute("types", types);
        model.addAttribute("page", blogService.listBlog(pageable, blogQo));
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
